﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Objects
{
    public class ValidationResult
    {
        public static readonly ValidationResult Null = new ValidationResult();
        public string Description;
        public int Code;
        private bool _IsSuccess = true;
        public virtual bool IsSuccess
        {
            get { return _IsSuccess && (Code == 0); }
            set { _IsSuccess = value; }
        }
        public override string ToString()
        {
            return Description;
        }
        public virtual IEnumerable<ValidationResult> GetAllResult()
        {
            if (this != Null)
                yield return this;
        }
    }

    public class ValidationResultList : ValidationResult, IEnumerable<ValidationResult>
    {
        public readonly List<ValidationResult> Inner = new List<ValidationResult>();
        public override bool IsSuccess
        {
            get
            {
                return base.IsSuccess;
            }
            set
            {
                base.IsSuccess = value;
            }
        }
        public override IEnumerable<ValidationResult> GetAllResult()
        {
            IEnumerable<ValidationResult> inner;
            if (Inner.Count == 0)
                yield return Null;
            else
                foreach (var item in Inner)
                    if ((inner = item as IEnumerable<ValidationResult>) != null)
                        foreach (var initem in inner)
                            yield return initem;
                    else
                        yield return item;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (Inner.Count > 0)
                sb.Append(Inner[0]);
            for (int i = 1; i < Inner.Count; i++)
            {
                sb.AppendLine();
                sb.Append(Inner[i]);
            }
            return sb.ToString();
        }

        #region IEnumerable<ValidationResult> Members

        public IEnumerator<ValidationResult> GetEnumerator()
        {
            return Inner.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
